package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Logger;

@Configuration
@EnableScheduling
@EnableAsync
public class TaskPoolConfig {

    private static final Logger logger = Logger.getLogger("TaskPoolConfig");

    @Bean("taskExecutor")
    public Executor asyncServiceExecutor(){
        logger.info("start asyncServiceExecutor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        int corePoolSize = Runtime.getRuntime().availableProcessors();
        //配置核心线程数
        executor.setCorePoolSize(corePoolSize*2);  //16
        //配置最大线程数
        executor.setMaxPoolSize(corePoolSize*2);   //32
        //配置队列大小
        executor.setQueueCapacity(99999);         //100
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix("async-service-jituan-");
        // rejection-policy：当pool已经达到max size的时候，并且队列已经满了，如何处理新任务
        // CALLER_RUNS：不在新线程中执行任务，而是有调用者所在的线程来执行
        //DiscardPolicy: 直接丢弃
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        //执行初始化
        executor.initialize();
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
}